package controller;

import bean.ColorCheckBean;
import bean.util.WindowUtil;

import com.sun.jna.platform.win32.WinDef.HWND;

public class RobotController {

	private HWND hwnd = null;

	private int reconnectCnt = 0;

	public RobotController(HWND hwnd) {
		this.hwnd = hwnd;
	}

	public int getGameStatus() {
		int gameStatus = -1;
		int checkerIndex = 0;

		while (gameStatus == -1 && checkerIndex < STATUS_CHECKERS.length) {
			ColorCheckBean[] checkerSet = STATUS_CHECKERS[checkerIndex++];
			boolean hasWrongPixel = false;

			for (ColorCheckBean checker : checkerSet) {
				if (!WindowUtil.checkPixelColor(hwnd, checker)) {
					hasWrongPixel = true;
					break;
				}
			}

			if (!hasWrongPixel) {
				gameStatus = checkerIndex - 1;
			}
		}

		return gameStatus;
	}

	public void moveOnMap(int direction, boolean isRun) {
		int x = 290;
		int y = 290;

		switch (direction) {
		case LEFT_DIRECTION:
			y = 240;
			break;
		case RIGHT_DIRECTION:
			x = 355;
			break;
		case UP_DIRECTION:
			x = 355;
			y = 240;
			break;
		case DOWN_DIRECTION:
			break;
		case NO_DIRECTION:
			x = 322;
			y = 265;
			break;
		default:
			return;
		}

		WindowUtil.mouseClick(hwnd, x, y, isRun);
	}

	public void setConnect() {
		reconnectCnt = 0;
	}

	public void reconnected() {
		reconnectCnt++;

		if (reconnectCnt >= 20) {
			delay(10000);
			WindowUtil.mouseClick(hwnd, 321, 303, true);

			delay(10000);
			WindowUtil.mouseClick(hwnd, 203, 150, true);

			delay(10000);
			WindowUtil.mouseClick(hwnd, 442, 360, true);

			setConnect();
		}
	}

	public void hitEnemyOnPosition(int position) {
		int[] location = ENEMY_POSITIONS[position];

		WindowUtil.mouseClick(hwnd, location[0], location[1], true);
	}

	public void roleActionSelect(int action) {
		int x = 380;
		int y = 80;

		switch (action) {
		case ATK_ACTION:
			y = 55;
			break;
		case DEF_ACTION:
			break;
		case ESC_ACTION:
			x = 600;
			break;
		default:
			return;
		}

		WindowUtil.mouseClick(hwnd, x, y, true);
	}

	public void delay(int ms) {
		WindowUtil.robot.delay(ms);
	}

	public static final int LEFT_DIRECTION = 0;

	public static final int RIGHT_DIRECTION = 1;

	public static final int UP_DIRECTION = 2;

	public static final int DOWN_DIRECTION = 3;

	public static final int NO_DIRECTION = 4;

	public static final int ATK_ACTION = 0;

	public static final int DEF_ACTION = 1;

	public static final int ESC_ACTION = 2;

	public static final int NORMAL_STATUS = 0;

	public static final int ROLE_WAR_STATUS = 1;

	public static final int PET_WAR_STATUS = 2;

	public static final int UNCONNECTED_STATUS = 3;

	private static final ColorCheckBean[][] STATUS_CHECKERS = {
			{ new ColorCheckBean(5, 70, -329747, -329747),
					new ColorCheckBean(100, 60, -329747, -329747),
					new ColorCheckBean(115, 70, -527128, -4212593),
					new ColorCheckBean(4, 78, -329747, -1185321) },
			{ new ColorCheckBean(385, 32, -197901, -4543844),
					new ColorCheckBean(636, 83, -329747, -330005),
					new ColorCheckBean(353, 78, -593697, -725285),
					new ColorCheckBean(423, 81, -329746, -395541) },
			{ new ColorCheckBean(385, 32, -3490384, -4083293),
					new ColorCheckBean(636, 83, -329747, -330005),
					new ColorCheckBean(353, 78, -593697, -725285),
					new ColorCheckBean(423, 81, -329746, -395541) },
			{ new ColorCheckBean(5, 70, -16777216, -16777216),
					new ColorCheckBean(100, 60, -16777216, -16777216),
					new ColorCheckBean(115, 70, -16777216, -16777216),
					new ColorCheckBean(4, 78, -16777216, -16777216) } };

	private static final int[][] ENEMY_POSITIONS = { { 160, 206 },
			{ 221, 257 }, { 95, 241 }, { 156, 289 }, { 226, 168 },
			{ 284, 218 }, { 32, 276 }, { 95, 326 }, { 287, 131 }, { 349, 182 } };

}
